﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Breeding;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Breeding
{
    /// <summary>
    /// 根据生产批次获取养殖信息请求命令处理类
    /// </summary>
    public class GetBreedingHandler:IRequestHandler<GetBreedingCommand,ApiResult<List<BreedingDTO>>>
    {
        IBaseRepository<Breeding_Information> breeRep;
        IMapper mapper;

        public GetBreedingHandler(IBaseRepository<Breeding_Information> breeRep, IMapper mapper)
        {
            this.breeRep = breeRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<BreedingDTO>>> Handle(GetBreedingCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<BreedingDTO>> res = new ApiResult<List<BreedingDTO>>();

                var breedings = breeRep.GetAll().Where(x => x.Production_Batch == request.production_Batch&&!x.IsDeleted);
                var model=mapper.Map<List<BreedingDTO>>(breedings);
                res.Msg = "根据生产批次获取养殖信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
